package com.bawei.module_mine.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_mine.R
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem

class ApplyForAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    companion object {
        const val TYPE_PARKING = 1 // 车位申请
        const val TYPE_VISITOR = 2 // 来访申请
        const val TYPE_REPAIR = 3 // 报修申请
        const val TYPE_PATROL = 4 // 巡更申请
    }
    private val carOrderList= mutableListOf<CarOrderModelItem>()
    private val visitOrderList= mutableListOf<VisitOrderModelItem>()
    private val repairOrderList= mutableListOf<RepairOrderModelItem>()
    private val inspectionOrderList= mutableListOf<InspectionOrderModelItem>()
    fun setCarData(data:List<CarOrderModelItem>){
        carOrderList.clear()
        if (data.isNotEmpty()) {
            carOrderList.add(data[0]) // 仅当 data 非空时添加
        }
        notifyDataSetChanged()
    }
    fun setVisitData(data:List<VisitOrderModelItem>){
        visitOrderList.clear()
        if (data.isNotEmpty()) {
            visitOrderList.add(data[0])
        }
        notifyDataSetChanged()
    }
    fun setRepairData(data:List<RepairOrderModelItem>){
        repairOrderList.clear()
        if (data.isNotEmpty()){
            repairOrderList.add(data[0])
        }
        notifyDataSetChanged()
    }
    fun setInspectionData(data:List<InspectionOrderModelItem>){
        inspectionOrderList.clear()
        if (data.isNotEmpty()){
            inspectionOrderList.add(data[0])
        }
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        return when(position){
            0 -> TYPE_PARKING
            1 -> TYPE_VISITOR
            2 -> TYPE_REPAIR
            3 -> TYPE_PATROL
            else -> {
                return TYPE_PARKING
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            TYPE_PARKING -> CarViewHolder(View.inflate(parent.context, R.layout.item_car_order, null))
            TYPE_VISITOR -> VisitViewHolder(View.inflate(parent.context, R.layout.item_visit_order, null))
            TYPE_REPAIR -> RepairViewHolder(View.inflate(parent.context, R.layout.item_repair_order, null))
            TYPE_PATROL -> InspectionViewHolder(View.inflate(parent.context,R.layout.item_inspection_order,null))
            else -> throw IllegalArgumentException("未知的视图类型")
        }
    }

    override fun getItemCount(): Int {
        return carOrderList.size+visitOrderList.size+repairOrderList.size+inspectionOrderList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        return when(holder){
            is CarViewHolder -> {
                if (carOrderList.isNotEmpty() && position < carOrderList.size){
                    val carOrder = carOrderList[position]
                    when(carOrder.status){
                        0 -> holder.carState.text="待审核"
                        1 -> holder.carState.text="已通过"
                        -1 -> holder.carState.text="已拒绝"
                    }
//                 根据id设置颜色（使用 ContextCompat 兼容不同API）
                    val colorRes = when (carOrder.status) {
                        0 -> com.bawei.lib_common.R.color.color_yellow_dark
                        1 -> com.bawei.lib_common.R.color.color_40b223
                        -1 -> com.bawei.lib_common.R.color.color_examine_red
                        else -> android.R.color.black // 默认颜色
                    }
                    holder.carState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.carText.text = "${carOrder.areaName}"
                    holder.carTime.text = "${carOrder.createTime.substring(0,10)} 08:50:30"
                }else{
                    // 无数据时隐藏或显示默认值
                    holder.carText.text = ""
                    holder.carTime.text = ""
                }
            }
            is VisitViewHolder -> {
                // 来访申请：position=1，对应 visitOrderList[0]（因为 position=0是车位，position=1是来访）
                val visitPosition = position - 1 // 0 -> 0, 1 -> 0（当总条目为2时）
                if (visitOrderList.isNotEmpty() && visitPosition < visitOrderList.size) {
                    val visitOrder = visitOrderList[visitPosition]
                    val colorRes = when (visitOrder.visitStatus) {
                        "待审核" -> com.bawei.lib_common.R.color.color_yellow_dark
                        "已通过" -> com.bawei.lib_common.R.color.color_40b223
                        "已完成" -> com.bawei.lib_common.R.color.color_40b223
                        "驳回" -> com.bawei.lib_common.R.color.color_examine_red
                        else -> android.R.color.black // 默认颜色
                    }
                    holder.visitState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.visitState.text = "${visitOrder.visitStatus}"
                    holder.visitText.text = "${visitOrder.visitorName}"
                    holder.visitTime.text = "${visitOrder.appointmentTime}"
                } else {
                    holder.visitState.text = ""
                    holder.visitText.text = ""
                    holder.visitTime.text = ""
                }
            }
            is RepairViewHolder -> {
                // 报修申请：position=2，对应 repairOrderList[0]（总条目≥3时）
                val repairPosition = position - 2 // 0->0, 1->0, 2->0（总条目≥3）
                if (repairOrderList.isNotEmpty() && repairPosition < repairOrderList.size) {
                    val repairOrder = repairOrderList[repairPosition]
                    val colorRes = when (repairOrder.processingStatus) {
                        "待处理" -> com.bawei.lib_common.R.color.color_308fff
                        "处理中"-> com.bawei.lib_common.R.color.color_yellow_dark
                        "已完成" -> com.bawei.lib_common.R.color.color_12aa00
                        else -> android.R.color.black // 默认颜色
                    }
                    holder.repairState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.repairState.text = "${repairOrder.processingStatus}"
                    holder.repairText.text = "${repairOrder.repairName}"
                    holder.repairTime.text = "${repairOrder.repairTime}"
                } else {
                    holder.repairState.text = ""
                    holder.repairText.text = ""
                    holder.repairTime.text = ""
                }
            }
            is InspectionViewHolder -> {
                // 巡更申请：position=3，对应 inspectionOrderList[0]（总条目≥4时）
                val inspectionPosition = position - 3 // 0->0, 1->0, 2->0, 3->0（总条目≥4）
                if (inspectionOrderList.isNotEmpty() && inspectionPosition < inspectionOrderList.size) {
                    val inspectionOrder = inspectionOrderList[inspectionPosition]
                    val colorRes = when (inspectionOrder.status) {
                        "新建" -> com.bawei.lib_common.R.color.color_308fff
                        "进行中"-> com.bawei.lib_common.R.color.color_yellow_dark
                        "已完成" -> com.bawei.lib_common.R.color.color_12aa00
                        "已转派" -> com.bawei.lib_common.R.color.color_examine_red
                        else -> android.R.color.black // 默认颜色
                    }
                    holder.inspectionState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.inspectionState.text = "${inspectionOrder.status}"
                    holder.inspectionText.text = "${inspectionOrder.address}"
                    holder.inspectionTime.text = "${inspectionOrder.appointmentTime.substring(0,10)} 10:58:10"
                } else {
                    holder.inspectionState.text = ""
                    holder.inspectionText.text = ""
                    holder.inspectionTime.text = ""
                }
            }
            else  -> {
            }
        }
    }
    class CarViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val carState: TextView = view.findViewById(R.id.item_car_state)
        val carText: TextView = view.findViewById(R.id.item_car_text)
        val carTime: TextView = view.findViewById(R.id.item_car_time)
    }
    class VisitViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val visitState: TextView = view.findViewById(R.id.item_visit_state)
        val visitText: TextView = view.findViewById(R.id.item_visit_text)
        val visitTime: TextView = view.findViewById(R.id.item_visit_time)
    }
    class RepairViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val repairState: TextView = view.findViewById(R.id.item_repair_state)
        val repairText: TextView = view.findViewById(R.id.item_repair_text)
        val repairTime: TextView = view.findViewById(R.id.item_repair_time)
    }
    class InspectionViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val inspectionState: TextView = view.findViewById(R.id.item_inspection_state)
        val inspectionText: TextView = view.findViewById(R.id.item_inspection_text)
        val inspectionTime: TextView = view.findViewById(R.id.item_inspection_time)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
//        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
//        recyclerView.addItemDecoration(GridSpaceItemDecoration(
//            horizontalSpacing = spacing,  // 水平间距（左右）
//            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
//        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}